package com.demo.personalaccountingbackend.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.personalaccountingbackend.dto.RecordDTO;
import com.demo.personalaccountingbackend.entity.AccountRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 类描述信息
 *
 * @author zgx
 * @version 1.0
 * @date 2025/10/15 11:40
 */

@Mapper
public interface AccountRecordMapper extends BaseMapper<AccountRecord> {

    /**
     * 根据ID查询记账记录（包含分类信息）
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.id = #{id}")
    RecordDTO findById(@Param("id") Long id);

    /**
     * 根据用户ID查询记账记录列表（包含分类信息）
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.user_id = #{userId} " +
            "ORDER BY r.record_date DESC, r.create_time DESC")
    List<RecordDTO> findRecordWithCategoryByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和日期范围查询记账记录（包含分类信息）
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.user_id = #{userId} AND r.record_date BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY r.record_date DESC, r.create_time DESC")
    List<RecordDTO> findRecordWithCategoryByUserIdAndDateRange(@Param("userId") Long userId,
                                                               @Param("startDate") LocalDate startDate,
                                                               @Param("endDate") LocalDate endDate);

    /**
     * 根据用户ID和类型查询记账记录（包含分类信息）
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.user_id = #{userId} AND r.type = #{type} " +
            "ORDER BY r.record_date DESC, r.create_time DESC")
    List<RecordDTO> findRecordWithCategoryByUserIdAndType(@Param("userId") Long userId,
                                                          @Param("type") Integer type);

    /**
     * 获取用户指定日期范围内的统计信息
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.user_id = #{userId} AND r.record_date BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY r.type, c.name")
    List<RecordDTO> getStatisticsByDateRange(@Param("userId") Long userId,
                                             @Param("startDate") LocalDate startDate,
                                             @Param("endDate") LocalDate endDate);

    /**
     * 获取用户月度统计
     */
    @Select("SELECT r.*, c.name as category_name, c.type as category_type, " +
            "c.icon as category_icon, c.color as category_color, c.sort as category_sort " +
            "FROM account_record r LEFT JOIN category c ON r.category_id = c.id " +
            "WHERE r.user_id = #{userId} AND YEAR(r.record_date) = #{year} " +
            "AND MONTH(r.record_date) = #{month} " +
            "ORDER BY r.record_date DESC")
    List<RecordDTO> getMonthlyStatistics(@Param("userId") Long userId,
                                         @Param("year") Integer year,
                                         @Param("month") Integer month);

//    /**
//     * 分页查询记账记录
//     */
//    IPage<RecordDTO> selectRecordPage(Page<RecordDTO> page,
//                                      @Param("ew") Wrapper<AccountRecord> wrapper);
}